/**
 * creation: 19-mei-2008
 */
package org.jfonia.util;

import java.util.HashMap;
import java.util.Map;

/**
 * Simple helper class for parsing key-value commandline arguments
 * (e.g. name=wijnand age=33)
 * 
 * @author wijnand.schepens@gmail.com
 *
 */
public class CommandParser
{
	/**
	 * Parses key-value pairs from a list of (command line) arguments.
	 * Key-value pairs are supposed to be specified as "xxx=yyy".
	 * 
	 * @param args list of arguments, e.g. from main(String[] args)
	 * @param startIndex index of the first argument to consider
	 * @return a hash-map of key-value pairs
	 * @throws Exception if syntax is not correct
	 */
	public static Map<String, String> parseCmdParams(String[] args, int startIndex) throws Exception
	{
		HashMap<String, String> map = new HashMap<String, String>();
		
		for (int i=startIndex; i<args.length; i++)
		{
			String [] parts = args[i].split("=");
			if (parts.length != 2)
				throw new Exception("parameter should be of the form 'xxx=yyy'");
			map.put(parts[0], parts[1]);
		}
		
		return map;
	}

}
